﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Krist_em_up.Components;
using Microsoft.Xna.Framework.Input;


namespace Krist_em_up.Interface
{
    public class OptionsMenu : GameScreen
    {
        protected List<SelectKeyButton> buttons;
        protected SpriteBatch spriteBatch;
        public SpriteFont font;
        protected GameStateManager game;
        protected int selectionIndex;
        protected TimeSpan lastAction, actionDelay;
        protected bool CanUpdate;
        protected Button retour;
        protected ControlsConfig ctrls1, ctrls2;



        public OptionsMenu(GameStateManager game, ControlsConfig ctrls1, ControlsConfig ctrls2)
            : base(game)
        {
            this.game = game;
            this.ctrls1 = ctrls1;
            this.ctrls2 = ctrls2;
        }
        public override void Initialize()
        {
            InitFromControlConfig();
            spriteBatch = new SpriteBatch(game.GraphicsDevice);
            selectionIndex = 0;
            font = game.Content.Load<SpriteFont>("Fonts/Gamefont");
            CanUpdate = false;
            actionDelay = new TimeSpan(0, 0, 0, 0, 300);
            lastAction = TimeSpan.Zero;
        }
        public override void LoadContent()
        {
            spriteBatch = new SpriteBatch(game.GraphicsDevice);
        }
        public override void UnloadContent()
        {

        }
        public override void Update(GameTime gameTime)
        {
            Clavier kb = game.getClavier();
            bool prev = CanUpdate;
            CanUpdate |= !kb.IsKeyDown(Keys.Enter);

            if (CanUpdate && (gameTime.TotalGameTime - lastAction > actionDelay))
            {
                if (prev != CanUpdate)
                    kb.Update(gameTime);
                if (selectionIndex == -1)
                {
                    retour.Update(gameTime);
                    if (kb.IsKeyDown(Keys.Enter))
                        retour.OnSelected();
                    if (kb.IsKeyDown(Keys.Up))
                        PrevButton(gameTime);
                    else if (kb.IsKeyDown(Keys.Down))
                        NextButton(gameTime);
                }
                else if (!buttons[selectionIndex].IsActive)
                {
                    if (kb.IsKeyDown(Keys.Up))
                        PrevButton(gameTime);
                    else if (kb.IsKeyDown(Keys.Down))
                        NextButton(gameTime);
                }
            }
            foreach (SelectKeyButton b in buttons)
            {
                if (b.HasFocus)
                    b.Update(kb, gameTime, this);
            }

        }
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.DrawString(font, "JOUEUR 1", Vector2.Zero, Color.White);
            spriteBatch.DrawString(font, "JOUEUR 2", new Vector2(0, 300), Color.White);
            foreach (SelectKeyButton b in buttons)
            {
                b.Draw(spriteBatch, font);
            }


            spriteBatch.End();
            retour.Draw(spriteBatch, font);


            //spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            //e.Begin();
            //e.CurrentTechnique.Passes[0].Begin();

            //spriteBatch.Draw(t, new Rectangle(0, 0, 100, 30), Color.White);

            //spriteBatch.End();
            //e.End();
            //e.CurrentTechnique.Passes[0].End();

        }

        public override void Reset()
        {

        }

        public void NextButton(GameTime gameTime)
        {
            // On est sur retour
            if (selectionIndex == -1)
            {

                selectionIndex = 0;
                retour.Unfocus();
                buttons[selectionIndex].Focus();
            }
            else
            {
                buttons[selectionIndex].Unfocus();
                selectionIndex++;
                // On est a la fin des configs
                if (selectionIndex >= buttons.Count)
                {
                    retour.Focus();
                    selectionIndex = -1;
                }
                else
                    buttons[selectionIndex].Focus();
            }

            lastAction = gameTime.TotalGameTime;
        }
        public void PrevButton(GameTime gameTime)
        {
            if (selectionIndex == -1)
            {
                retour.Unfocus();
                selectionIndex = buttons.Count - 1;
                buttons[selectionIndex].Focus();
                retour.Unfocus();
            }
            else
            {
                buttons[selectionIndex].Unfocus();
                selectionIndex--;
                if (selectionIndex < 0)
                {
                    buttons[0].Unfocus();
                    retour.Focus();
                }
                else
                {
                    buttons[selectionIndex].Focus();
                }
            }
            lastAction = gameTime.TotalGameTime;
        }
        public void UpdateLastAction(GameTime gametime)
        {
            lastAction = gametime.TotalGameTime;
        }
        /// <summary>
        /// Initialize buttons from the given Controls
        /// </summary>
        protected void InitFromControlConfig()
        {
            buttons = new List<SelectKeyButton>();
            Vector2 pos = new Vector2(0, 0);
            pos.Y = +50;
            SelectKeyButton b = GenerateButton(pos, "Gauche", ctrls1.KeyLeft);
            b.Focus();
            pos.Y += 50;
            GenerateButton(pos, "Droite", ctrls1.KeyRight);
            pos.Y += 50;
            GenerateButton(pos, "Saut", ctrls1.KeyJump);
            pos.Y += 50;
            GenerateButton(pos, "Tir", ctrls1.KeyShoot);
            pos.Y += 50;
            GenerateButton(pos, "Changer d'arme", ctrls1.KeyChange);
            pos.Y += 100;
            GenerateButton(pos, "Gauche", ctrls2.KeyLeft);
            pos.Y += 50;
            GenerateButton(pos, "Droite", ctrls2.KeyRight);
            pos.Y += 50;
            GenerateButton(pos, "Saut", ctrls2.KeyJump);
            pos.Y += 50;
            GenerateButton(pos, "Tir", ctrls2.KeyShoot);
            pos.Y += 50;
            GenerateButton(pos, "Changer d'arme", ctrls2.KeyChange);
            pos.Y += 50;
            retour = new Button(pos, "Sauver");
            retour.Selected += OnReturn;
        }
        protected SelectKeyButton GenerateButton(Vector2 pos, string label, Keys key)
        {
            SelectKeyButton b = new SelectKeyButton(pos, label, key, game);
            buttons.Add(b);
            return b;
        }
        protected void AssignBounds()
        {
            ctrls1.KeyLeft = buttons[0].BoundKey;
            ctrls1.KeyRight = buttons[1].BoundKey;
            ctrls1.KeyJump = buttons[2].BoundKey;
            ctrls1.KeyShoot = buttons[3].BoundKey;
            ctrls1.KeyChange = buttons[4].BoundKey;
            ctrls2.KeyLeft = buttons[5].BoundKey;
            ctrls2.KeyRight = buttons[6].BoundKey;
            ctrls2.KeyJump = buttons[7].BoundKey;
            ctrls2.KeyShoot = buttons[8].BoundKey;
            ctrls2.KeyChange = buttons[9].BoundKey;
        }
        void OnReturn(object sender, EventArgs e)
        {
            AssignBounds();
            game.SetControls(ctrls1,ctrls2);
            game.LeaveOptMenu();
        }
    }
}
